﻿using System.Security.Cryptography;
using System.Text;

/***
 *  SHA256加密
 *  
 *  方法：
 *  1.加密字节数组
 *  2.加密字符串
 *  
 *	时间：2021.11.24
 *	版本:1.0
 *	作者：李勇
 *	变更描述：创 建
 */

namespace HardwareInfo
{
    public static class Encryption
    {
        /// <summary>
        /// 使用SHA256加密字节数组
        /// </summary>
        /// <param name="_byter">要加密的字节数组</param>
        /// <returns>加密后的字节数组</returns>
        public static byte[] Sha256Bytes(byte[] _byter)
        {
            return SHA256.Create().ComputeHash(_byter);
        }

        /// <summary>
        /// 使用SHA256加密字符串
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Sha256String(string str)
        {
            StringBuilder s = new StringBuilder();
            byte[] shabyte = Sha256Bytes(Encoding.UTF8.GetBytes(str));
            foreach (var item in shabyte)
            {
                s.Append(item.ToString("x2"));
            }
            return s.ToString();
        }
    }
}
